package com.aimuceliang.aimuceliang.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by sqluo on 2017/6/8.
 */

public class TimeUtil {
    public static String getYMDHMSStrDate(String longStr) {
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long t = Long.parseLong(longStr);
            Date date = new Date(t);
            return sdfs.format(date);

        } catch (Exception e) {
            return "";
        }

    }

    public static String getYMDHMSStrDate(long time) {
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = new Date(time);
            return sdfs.format(date);

        } catch (Exception e) {
            return "";
        }

    }

    /**
     * 存储时间格式
     *
     * @param l
     * @return
     */
    public static String saveTime(long l) {
        return getYMDHMSStrDate(l);
    }


    public static int getIntFromStr(String time) {
        try {
            return Integer.parseInt(time);
        } catch (Exception e) {
            return 0;
        }
    }


    public static float getFloatFromStr(String fstr) {
        try {
            return ((int) (Float.parseFloat(fstr) * 100)) / 100f;
        } catch (Exception e) {
            return 0;
        }
    }

    public static String getYMDStr(long time) {
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdfs.format(new Date(time));
        } catch (Exception e) {
            return "";
        }

    }

    public static int get2StyleFromHexStr(String str) {
        int a = Integer.parseInt(str, 16);
        String as = Integer.toBinaryString(a);
        int len = Integer.parseInt(as);
        int sss = as.length();
        len = len / ((sss - 1) * 10) * ((sss / 2) * 10);
        return len;
    }

    public static int getIntFromHstr(String str) {
        return Integer.parseInt(str, 2);
    }

    public static int getIntFromBstr(String str) {
        return Integer.parseInt(str, 16) - 48;
    }

    public static int get2StyleFromHexStr1(String str) {
        if (str.length() > 3) {
            String point = str.substring(0, 3);
            return getIntFromHstr(point);
        }
        return 0;
    }

    public static float getScale(int value) {
        int data = 1;
		
		//add by 毛江常州
		if(value == 1)
		{
			return 1;
		}
		//add by 毛江常州
        for (int i = 0; i < value; i++) {
            data = data * 10;
        }
        return data / 100f;
    }

    public static String getHstrFrom16str(String str) {
        int a = Integer.parseInt(str, 16);
        return Integer.toBinaryString(a);

    }

    public static int get2StyleFromHexStr2(String str) {
        if (str.length() > 5) {
            String point = str.substring(3, 6);
            return getIntFromHstr(point);
        }
        return 0;
    }

    public static int getIntFromByte(Byte data) {
        return data & 0xff;
    }

    public static String getStrFromList(ArrayList<Byte> ps) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ps.size(); i++) {
            sb.append(ps.get(i) + " ");
        }

        return sb.toString();
    }

    public static float getAverage(ArrayList<Float> datas) {
        float ave = 0;
        if (datas.size() > 0) ave = getSum(datas) / datas.size();


        return ((int) (ave * 100)) / 100f;
    }
    public static float getSum(ArrayList<Float> datas) {
        float sum = 0;
        for (Float data : datas) {
            sum += data;
        }
        return sum;
    }

}
